/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.cjb.malacma.rssfeed.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.PostLoad;
import javax.persistence.PrePersist;

/**
 *
 * @author malacma
 */
@NamedQueries({
    @NamedQuery(name = "FeedURL.findAllFeeds", query = "SELECT c FROM FeedUrl c"),
    @NamedQuery(name = "FeedURL.findOffline", query = "SELECT c FROM FeedUrl c WHERE c.isOnline = '0'"),
    @NamedQuery(name = "FeedURL.findByTitle", query = "SELECT c FROM FeedUrl c WHERE c.titulo = :titulo"),
    @NamedQuery(name = "FeedURL.findByTitleLike", query = "SELECT c FROM FeedUrl c WHERE UPPER(c.titulo) LIKE :titulo"),
    @NamedQuery(name = "FeedURL.findByTitleByTag", query = "SELECT DISTINCT c FROM FeedUrl c JOIN c.tags d WHERE UPPER(c.titulo) LIKE :titulo"),
    //@NamedQuery(name = "FeedURL.findByTitleLike", query = "SELECT c FROM FeedUrl c WHERE c.titulo LIKE :titulo"),
    @NamedQuery(name = "FeedURL.findLastFeed", query = "SELECT c FROM FeedUrl c WHERE c.id = (SELECT MAX(c.id) FROM c)")
})
@Entity
public class FeedUrl implements Serializable , Comparable<FeedUrl>{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    private Long id;
    
    @Column(length = 255, unique = true, nullable = false)
    private String url = "";
    
    @Column(length = 255, unique = true, nullable = false)
    private String titulo = "";
    
    @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private List<Tag> tags = new ArrayList<Tag>();
    
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<Comment> lComments = new ArrayList<Comment>();
    
    @Column(length = 1, nullable = false)
    private char isOnline = '0';

    @Column(length = 1, nullable = false)
    private char isEnabled = '0';

    public char getIsEnabled() {
        return isEnabled;
    }

    public void setIsEnabled(char isEnabled) {
        this.isEnabled = isEnabled;
    }

    public List<Comment> getlComments() {
        return lComments;
    }

    public void setlComments(List<Comment> lComments) {
        this.lComments = lComments;
    }

    


    public boolean isOnline() {
        return isOnline == '1' ? true : false;
    }

    public void setOnline() {
        this.isOnline = '1';
    }

    public List<Comment> getLComments() {
        return lComments;
    }

    public void setLComments(List<Comment> lComments) {
        this.lComments = lComments;
    }

    public char getIsOnline() {
        return isOnline;
    }

    public void setIsOnline(char isOnline) {
        this.isOnline = isOnline;
    }

    public List<Tag> getTags() {
        return tags;
    }

    public void setTags(List<Tag> tags) {
        this.tags = tags;
    }
    //Before safe changes null titulos

    @PrePersist
    public void prePersist() {
        titulo = titulo == null ? "?????????????" : titulo;
    }

    //After load remove line break from titulo
    @PostLoad
    public void postLoad() {
        titulo = titulo.replaceAll("(\r\n|\r|\n|\n\r)", " ");
    }

    public String getTitulo() {
        return titulo.toUpperCase();
    }

    public void setTitulo(String titulo) {
        this.titulo = titulo.toUpperCase();
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof FeedUrl)) {
            return false;
        }
        FeedUrl other = (FeedUrl) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "net.cjb.malacma.rssfeed.entity.FeedUrl[id=" + id + "]";
    }

    public int compareTo(FeedUrl arg0) {
        int lastCmp = this.titulo.compareTo(arg0.getTitulo());
        return (lastCmp != 0 ? lastCmp : this.id.compareTo(arg0.getId()));

    }
}
